home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / SmallTalk / PositionableStream.st < prev    next >
Text File  |  1995-08-25  |  5KB  |  204 lines

  1. "======================================================================
  2. |
  3. |   PositionableStream Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbyrne     19 Sep 89      Converted to use real method categories
  36. |
  37. | sbyrne     25 Apr 89      created.
  38. |
  39. "
  40.  
  41. Stream subclass: #PositionableStream
  42.        instanceVariableNames: 'collection ptr endPtr access'
  43.        classVariableNames: ''
  44.        poolDictionaries: ''
  45.        category: nil
  46. !
  47.  
  48. PositionableStream comment: 
  49. 'My instances represent streams where explicit positioning is permitted.
  50. Thus, my streams act in a manner to normal disk files: you can read
  51. or write sequentially, but also position the file to a particular place
  52. whenever you choose.  Generally, you''ll want to use ReadStream, WriteStream
  53. or ReadWriteStream instead of me to create and use streams.' !
  54.  
  55.  
  56. !PositionableStream class methodsFor: 'instance creation'!
  57.  
  58. on: aCollection
  59.     self subclassResponsiblity
  60. !
  61.  
  62. on: aCollection from: firstIndex to: lastIndex
  63.     ^self on: (aCollection copyFrom: firstIndex to: lastIndex)
  64.  
  65. !!
  66.  
  67.  
  68.  
  69. !PositionableStream methodsFor: 'accessing-reading'!
  70.  
  71. next
  72.     | element |
  73.     (access bitAnd: 1) = 0
  74.         ifTrue: [ ^self shouldNotImplement ].
  75.     self atEnd ifTrue: [ ^self error: 'end of stream reached' ].
  76.     element _ collection at: ptr.
  77.     ptr _ ptr + 1.
  78.     ^element
  79. !
  80.  
  81. next: anInteger
  82.     "Returns a collection of the same type that the stream accesses, that has
  83.     the next anInteger elements from the stream."
  84.     | newStream |
  85.     newStream _ WriteStream on: (collection species new: 0).
  86.     anInteger timesRepeat: [ newStream nextPut: (self next) ].
  87.     ^newStream contents
  88. !
  89.  
  90. peek
  91.     "Returns the next element of the stream without moving the pointer.
  92.     Returns nil when at end of stream."
  93.     | peekValue |
  94.     self atEnd ifTrue: [ ^nil ].
  95.     peekValue _ self next.
  96.     self skip: -1.
  97.     ^peekValue    
  98. !
  99.  
  100. peekFor: anObject
  101.     "Returns true and gobbles the next element from the stream of it is
  102.     equal to anObject, returns false and doesn't gobble the next element
  103.     if the next element is not equal to anObject."
  104.     (self peek) = anObject
  105.         ifTrue: [ self next.
  106.               ^true ]
  107.         ifFalse: [ ^false ]
  108. !
  109.  
  110. upTo: anObject
  111.     "Returns a collection of the same type that the stream accesses, up to 
  112.     but not including the object anObject.  Returns the entire rest of the 
  113.     stream's contents if anObject is not present.  ### Should this use = or
  114.     ==."
  115.     | newStream |
  116.     newStream _ WriteStream on: (collection species new: 0).
  117.     [ self atEnd or: [ self peek ~= anObject ] ] whileFalse:
  118.         [ newStream nextPut: (self next) ].
  119.     ^newStream contents
  120. !
  121.  
  122. contents
  123.     "Returns a collection of the same type that the stream accesses, up to 
  124.     and including the final element."
  125.     ^collection copyFrom: 1 to: endPtr
  126. !
  127.  
  128. reverseContents
  129.     "Returns a collection of the same type that the stream accesses, up to 
  130.     and including the final element, but in reverse order."
  131.     | newCollection |
  132.     newCollection _ collection species new: endPtr.
  133.     1 to: endPtr do:
  134.         [ :i | newCollection at: i put: (collection at: endPtr - i + 1) ].
  135.     ^newCollection
  136. !!
  137.  
  138.  
  139.  
  140. !PositionableStream methodsFor: 'testing'!
  141.  
  142. atEnd
  143.     ^ptr > endPtr
  144. !
  145.  
  146. isEmpty
  147.     ^endPtr = 0
  148. !!
  149.  
  150.  
  151.  
  152. !PositionableStream methodsFor: 'enumerating'!
  153.  
  154. !
  155.  
  156.  
  157.  
  158. !PositionableStream methodsFor: 'positioning'!
  159.  
  160. position
  161.     ^ptr
  162. !
  163.  
  164. position: anInteger
  165.     (anInteger between: 1 and: endPtr)
  166.     ifTrue: [ ptr _ anInteger ]
  167.     ifFalse: [ ^self error: 'position out of range' ]
  168. !
  169.  
  170. reset
  171.     ptr _ 1
  172. !
  173.  
  174. setToEnd
  175.     ptr _ endPtr + 1
  176. !
  177.  
  178. skip: anInteger
  179.     ptr _ (ptr + anInteger max: 1) min: endPtr
  180. !
  181.  
  182. skipTo: anObject
  183.     "Moves the current position to after the next occurrence of anObject
  184.     and returns true if anObject was found.  If anObject doesn't exist, the 
  185.     position is unchanged, and false is returned."
  186.     | curPos |
  187.     curPos _ self position.
  188.     [ self atEnd ] whileFalse:
  189.         [ (self nextMatchFor: anObject)
  190.         ifTrue: [ ^true ] ].
  191.     self position: curPos.
  192.     ^false    
  193.  
  194. !!
  195.  
  196.  
  197. !PositionableStream methodsFor: 'private'!
  198.  
  199. access: anInteger
  200.     access _ anInteger
  201. !!
  202.